/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/


import { IAntdTablePageParams, IPromiseApi, IPromiseError, IPromiseListApi } from '@/types/type';
import { AntdFormUtils } from 'ahooks/es/useAntdTable/types';
import request from '@/utils/request';
import {
  ICategoryEditCreate,
  ICategoryEditParams,
  ICategoryItem,
  ICategoryList,
  IStatus,
} from '@/types/sceneCategory';
import { paginationParams } from '@/utils/soa/utils';
import base from './base';

// 场景分类列表
export async function getCategoryList(
  { current, pageSize }: IAntdTablePageParams,
  formData: AntdFormUtils,
) {
  const { data } = await request.get<IPromiseListApi<ICategoryList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/category/page`,
    {
      params: {
        ...formData,
        ...paginationParams(current, pageSize),
      },
    },
  );
  return {
    list: data?.itemSize
      ? data?.dataList.map((item: ICategoryItem, index: number) => ({
          ...item,
          index: index + 1 + (current - 1) * pageSize,
        }))
      : [],
    total: data?.pageCount,
  };
}

// 修改场景分类
export async function categoryEdit(data: ICategoryEditParams, id: number) {
  return await request.put<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/category`,
    {
      data: {
        ...data,
        id,
      },
    },
  );
}

// 分类状态更改
export async function categoryStatusEdit(status: IStatus, id: number) {
  return await request.put<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/category/status`,
    {
      data: {
        status,
        id,
      },
    },
  );
}

// 删除场景分类
export async function categoryDelete(id: number) {
  return await request.delete(`/${base.API}/ms-soa-scene/${base.VERSION}/admin/category`, {
    params: {
      id,
    },
  });
}

// 新增场景分类
export async function categoryCreate(params: ICategoryEditCreate) {
  return await request.post<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/category`,
    {
      data: params,
    },
  );
}

// 场景分类数据查询
export async function getCategory(name: string) {
  return await request.get<IPromiseApi<ICategoryList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/category`,
    {
      params: {
        name,
      },
    },
  );
}
